/**
 *  ClassName: DaoFactory.java
 *  created on 2014-05-23
 *  Copyrights 2011-2012 ITVK.CN All rights reserved.
 *  site: http://WWW.ITVK.CN
 *  email: qjyong@gmail.com
 */
package com.lxq.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 带缓存的Dao工厂
 * @author qiujy
 */
public class DaoFactory {
	private static Properties props = new Properties();
	
	/**用于缓存dao实例的Map*/
	private static Map<String,Object> cache
			= Collections.synchronizedMap(new HashMap<String, Object>());
	
	private DaoFactory(){}
	
	static {
		try {
			InputStream is = Thread.currentThread()
					.getContextClassLoader()
					.getResourceAsStream("dao.properties");
			
			props.load(is);
		} catch (IOException e) {
			System.err.println("在classpath下没有找到dao.properties文件，请检查!");
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据dao.properties配置文件中的dao名称，创建或获取它的实现类实例
	 * @param daoName dao.properties配置文件中的dao名称
	 * @return 操作成功，返回对应的实现类实例；否则返回null
	 */
	public static Object getInstance(String daoName){
		Object o = null;
		
		o = cache.get(daoName); //先根据dao名称去缓存Map中取对应的实例
		
		if(null == o){ //缓存中还不存在，就新创建，并存放到缓存中。
			String className = props.getProperty(daoName);
			
			if(null != className && !"".equals(className)){
				try {
					o = Class.forName(className).newInstance();
					
					cache.put(daoName, o);
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
		
		return o;
	}
	
	/**
	 * 根据dao.properties配置文件中的dao名称，创建或获取它的实现类实例
	 * @param daoName dao.properties配置文件中的dao名称
	 * @param clazz 对应的类型信息对象
	 * @return 操作成功，返回对应的实现类实例；否则返回null
	 */
	public static <T> T getInstance(String daoName, Class<T> clazz){
		T t = null;
		
		Object temp = cache.get(daoName);
		if(null == temp){
			String className = props.getProperty(daoName);
			if(null != className && !"".equals(className)){
				try {
					Object o = Class.forName(className).newInstance();
					
					t = clazz.cast(o);
					
					cache.put(daoName, t);
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}else{
			t = clazz.cast(temp);
		}
		return t;
	}
}
